package aes.gui;

import java.awt.CardLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSeparator;
import javax.swing.JTextField;

import aes.general.StudentControl;



/**
 * The Class StudentMakeExam. This Panel represent the part of Student UI.
 * 	In this Panel Student choose the mode of Exam execution - Manual or Automatic and enters the Activation code of Exam given to him by teacher
 */
public class StudentMakeExam extends JPanel implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jp;
	private JPanel StudentExamPanel;
	private JLabel l1;
	private JLabel l2;
	private JButton b1;
	private JButton b2;
	private JRadioButton rd1;
	private JRadioButton rd2;
	private ButtonGroup group;
	private JSeparator separator_1;
	private JLabel l3;
	private JTextField textField;
	private int mode;
	
	/** The Activation code flag. */
	public static boolean ActivationCodeFlag = false;
	
	/**
	 * Create the panel.
	 *
	 * @param contentPane the content pane of Main Student Frame
	 */
	public StudentMakeExam(JPanel contentPane) {
		setLayout(null);
		this.jp = contentPane;
		ActivationCodeFlag = false;
		initialize();	
	}
	
	
	private void initialize() {
		setLayout(null);
		
	
		
		l1 = new JLabel("Exam Mode");
		l1.setFont(new Font("Tempus Sans ITC", Font.BOLD, 21));
		l1.setBounds(253, 11, 133, 29);
		add(l1);
		
		l2 = new JLabel("Choose a mode:");
		l2.setFont(new Font("Tahoma", Font.PLAIN, 13));
		l2.setBounds(271, 118, 97, 30);
		add(l2);
		
		b1 = new JButton("Next");
		b1.setBounds(342, 396, 110, 23);
		add(b1);
		b1.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				if (group.isSelected(null)) {
				JOptionPane.showMessageDialog(null, "Error, Must select mode ", "Error",
						JOptionPane.ERROR_MESSAGE);
				}
				else if(textField.getText().trim().length() == 0){
					JOptionPane.showMessageDialog(null, "Error, Must Enter Activation code ", "Error",
							JOptionPane.ERROR_MESSAGE);
				}
				else if(mode == 1){  //Manual selected
						MakeExamManual();
						textField.setText(null);
				}
				else if(mode == 2){	
					MakeExamAutomati();
					textField.setText(null);
						}
				
				}
		});
		
		b2 = new JButton("Back");
		b2.setBounds(206, 396, 110, 23);
		add(b2);
		b2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ActivationCodeFlag = false;
				textField.setText(null);
				group.clearSelection();
				CardLayout cl = (CardLayout)(jp.getLayout());
				 cl.show(jp,"MainPanel");	}
		});
		
		JLayeredPane layeredPane = new JLayeredPane();
		layeredPane.setBounds(206, 155, 228, 38);
		add(layeredPane);
				
		rd1 = new JRadioButton("Manual");
		rd1.setBounds(136, 7, 86, 23);
		layeredPane.add(rd1);
		
		rd2 = new JRadioButton("Automatic");
		rd2.setBounds(20, 7, 83, 23);
		layeredPane.add(rd2);
		
		group = new ButtonGroup();
		group.add(rd1);
		group.add(rd2);
		
		rd1.addActionListener(this);
		rd2.addActionListener(this);
		
		
		separator_1 = new JSeparator();
		separator_1.setBounds(10, 51, 620, 2);
		add(separator_1);
		
		l3 = new JLabel("Enter Exam Activation Code:");
		l3.setFont(new Font("Tahoma", Font.PLAIN, 13));
		l3.setBounds(162, 255, 189, 30);
		add(l3);
		
		textField = new JTextField();
		textField.setBounds(361, 261, 64, 20);
		add(textField);
	}


	/**
	 * Make exam automatic. Opens another element of Student UI . Called if student had chosen automatic exam execution mode
	 */
	protected void MakeExamAutomati()
	{
		
	
		StudentControl.CheckActivationCodeID(textField.getText());
		String ActivCode = textField.getText();
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			
			e.printStackTrace();
		}
		
		if (ActivationCodeFlag==true)
		{	
			ActivationCodeFlag = false;
			StudentExamPanel = new StudentExamPanel(jp , ActivCode);
			jp.add(StudentExamPanel, "StudentExamPanel");
			CardLayout cl = (CardLayout)(jp.getLayout());		
			cl.show(jp,"StudentExamPanel");							
		}
		
		else {	JOptionPane.showMessageDialog(null, "Error, Wrong Activation Code ", "Error",
				JOptionPane.ERROR_MESSAGE);}
		
		
	}


	/**
	 * Make exam manual.  Opens another element of Student UI . Called if student had chosen manual exam execution mode
	 */
	protected void MakeExamManual() {
	
		StudentControl.CheckActivationCodeID(textField.getText());
		String ActivCode = textField.getText();
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			
			e.printStackTrace();
		}
		
		
		if (ActivationCodeFlag==true)
		{		
			ActivationCodeFlag = false;
			StudentMakeExamManualPane MakeExamManual = new StudentMakeExamManualPane(jp,ActivCode);
			jp.add(MakeExamManual, "StudentMakeExamManualPane");
			CardLayout cl = (CardLayout)(jp.getLayout());		
			cl.show(jp,"StudentMakeExamManualPane");
		}
		
		else {	JOptionPane.showMessageDialog(null, "Error, Wrong Activation Code ", "Error",
				JOptionPane.ERROR_MESSAGE);}
		
	}


	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == rd1)
			 mode = 1;
		else if(e.getSource() == rd2)
			 mode = 2; 
	}
}
